package com.frostmourne.mxsattributeinventory;

import com.frostmourne.mxsattributeinventory.commands.MainCommand;
import com.frostmourne.mxsattributeinventory.hooks.PAPIHook;
import com.frostmourne.mxsattributeinventory.listeners.ExpandListener;
import com.frostmourne.mxsattributeinventory.listeners.InventoryListener;
import com.frostmourne.mxsattributeinventory.listeners.PlayerListener;
import com.frostmourne.mxsattributeinventory.modules.extra.BroadCastScript;
import com.frostmourne.mxsattributeinventory.modules.extra.CancelEvent;
import com.frostmourne.mxsattributeinventory.modules.extra.DefaultCurrentType;
import com.frostmourne.mxsattributeinventory.utils.FileUtils;
import com.frostmourne.mxsattributeinventory.utils.MsgUtils;
import com.gitee.mxstars.builder.InventoryBuilder;
import com.gitee.mxstars.mxscript.moudle.BaseScript;
import com.gitee.mxstars.mxscript.moudle.PlayerCommand;
import com.gitee.mxstars.sql.STable;
import com.gitee.mxstars.utils.MySQLUtils;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.Inventory;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.*;

public final class MXSAttributeInventory extends JavaPlugin {

    private static MXSAttributeInventory plugin;
    private static boolean hasDA;


    public static String PREFIX;
    public static String savePath;
    public static Map<String, InventoryBuilder> inventoryMap = new HashMap<>();
    public static Map<String, ConfigurationSection> scriptMap = new HashMap<>();
    public static Map<String,String> titleMap = new HashMap<>();
    public static Map<String, List<String>> enableMap = new HashMap<>();
    public static String ATTR;



    public static MXSAttributeInventory getPlugin() {
        return plugin;
    }

    public static boolean isHasDA() {
        return hasDA;
    }

    public static void setHasDA(boolean hasDA) {
        MXSAttributeInventory.hasDA = hasDA;
    }


    @Override
    public void onEnable() {
    plugin = this;
    Bukkit.getPluginManager().registerEvents(new InventoryListener(),this);
    Bukkit.getPluginManager().registerEvents(new PlayerListener(),this);
    Bukkit.getPluginCommand("mxsai").setExecutor(new MainCommand());
    new PAPIHook().register();
    loadScripts();
        hasDA = getConfig().getBoolean("Support.DragonArmourers",false);
        if (hasDA){
            Bukkit.getPluginManager().registerEvents(new ExpandListener(),this);
        }
    switch (getConfig().getString("SavePath","default").toUpperCase()){
        case "DEFAULT":
            savePath = getDataFolder() + "/Data";
            break;
        default:
            savePath = getConfig().getString("SavePath","default");
            break;
    }
    load();
    }

    public void load(){
        saveDefaultConfig();
        reloadConfig();
        inventoryMap.clear();
        scriptMap.clear();
        titleMap.clear();
        ATTR = getConfig().getString("AttrPlugin","none");
        PREFIX = getConfig().getString("Msg.Prefix").replace("&","§");
        File folder = new File(getDataFolder() + "/Inv");
        if (!folder.exists()){
            saveResource("Inv/Example.yml",false);
        }
        if (folder.listFiles() != null){
            for (File f : FileUtils.getFiles(folder.getPath())){
                FileConfiguration config = YamlConfiguration.loadConfiguration(f);
                String fileName = f.getName().replace(".yml","");
                InventoryBuilder inventoryBuilder = new InventoryBuilder(f);

                inventoryMap.put(fileName,inventoryBuilder);
                titleMap.put(inventoryBuilder.getInv().getTitle(),fileName);
                if (config.getKeys(false).contains("Script")){
                    scriptMap.put(fileName,config.getConfigurationSection("Script"));
                }

                List<String> options = new ArrayList<>();
                if (config.getKeys(false).contains("Options")){
                    ConfigurationSection cs = config.getConfigurationSection("Options");
                    for (String s : config.getConfigurationSection("Options").getKeys(false)){
                        switch (s.toUpperCase()){
                            case "ATTRIBUTE":
                                if (cs.getBoolean(s)){
                                    options.add("Attribute");
                                }
                                break;
                            case "DRAGONARMOURERS":

                                if (cs.getBoolean(s)){
                                    options.add("DragonArmourers");
                                }
                                break;
                            default:
                                break;
                        }
                    }
                }

                enableMap.put(fileName,options);

                MsgUtils.sendMsg(Bukkit.getConsoleSender(),"&f界面载入完成: &6" + fileName);
            }
            MsgUtils.sendMsg(Bukkit.getConsoleSender(),"");
            MsgUtils.sendMsg(Bukkit.getConsoleSender(),"&f配置已全部载入完成");
            MsgUtils.sendMsg(Bukkit.getConsoleSender(),"&f龙时挂钩状态: &6" + (hasDA ? "开启" : "关闭"));
            MsgUtils.sendMsg(Bukkit.getConsoleSender(),"&f当前属性插件: &6" + ATTR);
            MsgUtils.sendMsg(Bukkit.getConsoleSender(),"&f当前储存路径: &6" + savePath);
            MsgUtils.sendMsg(Bukkit.getConsoleSender(),"&fBy Frostmourne and baka_gua");
        }


    }

    public void loadScripts(){
        new BroadCastScript();
        new DefaultCurrentType();
        new PlayerCommand();
        new CancelEvent();
        new BaseScript();
    }




    @Override
    public void onDisable() {
        // Plugin shutdown logic
    }
}
